package net.tngou.iask.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.jdbc.QueryHelper;
import net.tngou.iask.pojo.Cure;
import net.tngou.iask.pojo.Department;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.pojo.Patient;
import net.tngou.iask.pojo.Place;
import net.tngou.iask.pojo.Treatment;
import net.tngou.iask.pojo.User;
import net.tngou.iask.util.StringUtil;

/**
 * 治疗
 * @author tngou@tngou.net
 *
 */
public class CureDao extends BaseDao {
	
	/**
	 * 更新治疗相关管理
	 * @param cure
	 * @param place
	 * @param treatment
	 * @param department
	 */
	public void update(long cure,Integer[] place,Integer[] treatment,Integer[] department,Long[] user) {
		
		//治疗对应实体部位
			
		if(place!=null)
		{
			String delete_sql="DELETE FROM `"+POJO.DABASE+"_cure_place` WHERE cure=?";
			String add_sql ="INSERT INTO `"+POJO.DABASE+"_cure_place`( `cure`, `place`) VALUES (?,?)";
			QueryHelper.update(delete_sql, cure);
			Object[][] params =new Object[place.length][2];
			for (int i = 0; i < place.length; i++) {
				params[i][0]=cure;
				params[i][1]=place[i];
			}
			QueryHelper.batch(add_sql, params );
		}

		//治疗关联治疗方法
		
		if(treatment!=null)
		{
			String delete_sql="DELETE FROM `"+POJO.DABASE+"_cure_treatment` WHERE cure=?";
			String add_sql ="INSERT INTO `"+POJO.DABASE+"_cure_treatment`( `cure`, `treatment`) VALUES (?,?)";
			QueryHelper.update(delete_sql, cure);	
			Object[][] params =new Object[treatment.length][2];
			for (int i = 0; i < treatment.length; i++) {
				params[i][0]=cure;
				params[i][1]=treatment[i];
			}
			QueryHelper.batch(add_sql, params );
		}
		
		//治疗关联科室
		
		if(department!=null)
		{
			String delete_sql="DELETE FROM `"+POJO.DABASE+"_cure_department` WHERE cure=?";
			String add_sql ="INSERT INTO `"+POJO.DABASE+"_cure_department`( `cure`, `department`) VALUES (?,?)";
			QueryHelper.update(delete_sql, cure);	
			Object[][] params =new Object[department.length][2];
			for (int i = 0; i < department.length; i++) {
				params[i][0]=cure;
				params[i][1]=department[i];
			}
			QueryHelper.batch(add_sql, params );
		}
		//治疗关联用户
		
		if(user!=null)
		{
			String delete_sql="DELETE FROM `"+POJO.DABASE+"_cure_user` WHERE cure=?";
			String add_sql ="INSERT INTO `"+POJO.DABASE+"_cure_user`( `cure`, `user`) VALUES (?,?)";
			QueryHelper.update(delete_sql, cure);	
			Object[][] params =new Object[user.length][2];
			for (int i = 0; i < user.length; i++) {
				params[i][0]=cure;
				params[i][1]=user[i];
			}
			QueryHelper.batch(add_sql, params );
		}		
	}
	
	
	/**
	 * 取得对应实体用户
	 * @param main 实体
	 * @param patient 病人ID
	 * @return
	 */
	public List<Map<String, Object>> getList(long main,long patient)  {
		String sql_r="SELECT cure.*,patient.name,patient.gender,patient.birthday,survey.name as  sname ,disease.name as dname ";
		String sql=" FROM `"+POJO.DABASE+"_cure` cure ,`"+POJO.DABASE+"_patient` patient ,`"+POJO.DABASE+"_survey` survey ,`"+POJO.DABASE+"_disease` disease  WHERE"
				+ " cure.patient=patient.id AND  survey.id=cure.survey AND disease.id=cure.disease AND cure.main=? AND patient.id=? ";			
		String sql_o=" ORDER BY cure.id DESC";//排序
		return QueryHelper.query(sql_r+sql+sql_o, main,patient);
	}
	
	/**
	 * 取得实体中的治疗
	 * @param page
	 * @param size
	 * @param main 实体
	 * @param keyword 关键词
	 * @return
	 */
	public PageUtil getPage(int page,int size,long main,String keyword) {
		List<Map<String, Object>> list=new ArrayList<>();
		int total=0;
		String sql_r="SELECT cure.*,patient.name,patient.gender,patient.no,patient.birthday,survey.name  as sname  ,disease.name as dname";
		String sql_t="SELECT COUNT(*) ";
		String sql=" FROM `"+POJO.DABASE+"_cure` cure ,`"+POJO.DABASE+"_patient` patient,`"+POJO.DABASE+"_survey` survey ,`"+POJO.DABASE+"_disease` disease WHERE"
				+ " cure.patient=patient.id AND  survey.id=cure.survey AND disease.id=cure.disease  AND cure.main=? ";
				
		String sql_o=" ORDER BY cure.id DESC";//排序
		if(StringUtil.isNotEmpty(keyword))
		{
			sql =sql+" AND (cure.number LIKE ? OR patient.name LIKE ?') " ;
			Object[] params={main};
			list=QueryHelper.query_page( sql_r+sql+sql_o, page, size, params);			
			total=(int) QueryHelper.stat(sql_t+sql, params);
		}else
		{
			Object[] params={main};
			list=QueryHelper.query_page( sql_r+sql+sql_o, page, size, params);			
			total=(int) QueryHelper.stat(sql_t+sql, params);
		}

		return new PageUtil(list, page, size, total);
	}
	
	public PageUtil getPage(int page,int size,long main,int  type ,String keyword) {
		List<Map<String, Object>> list=new ArrayList<>();
		int total=0;
		String sql_r="SELECT cure.*,patient.name,patient.gender,patient.no,patient.birthday,survey.name  as sname  ,disease.name as dname";
		String sql_t="SELECT COUNT(*) ";
		String sql=" FROM `"+POJO.DABASE+"_cure` cure ,`"+POJO.DABASE+"_patient` patient,`"+POJO.DABASE+"_survey` survey ,`"+POJO.DABASE+"_disease` disease WHERE"
				+ " cure.patient=patient.id AND  survey.id=cure.survey AND disease.id=cure.disease  AND cure.main=?  AND cure.type=?";
				
		String sql_o=" ORDER BY cure.id DESC";//排序
		if(StringUtil.isNotEmpty(keyword))
		{
			sql =sql+" AND (cure.number LIKE ? OR patient.name LIKE ?') " ;
			Object[] params={main,type,"%"+keyword+"%","%"+keyword+"%"};
			list=QueryHelper.query_page( sql_r+sql+sql_o, page, size, params);			
			total=(int) QueryHelper.stat(sql_t+sql, params);
		}else
		{
			Object[] params={main,type};
			list=QueryHelper.query_page( sql_r+sql+sql_o, page, size, params);			
			total=(int) QueryHelper.stat(sql_t+sql, params);
		}

		return new PageUtil(list, page, size, total);
	}
	
	
	/**
	 * 取得治疗 疾病对应信息
	 * @param page
	 * @param size
	 * @param main
	 * @param disease
	 * @return
	 */
	public PageUtil getPage(int page,int size,long main,long disease) {
		List<Map<String, Object>> list=new ArrayList<>();
		int total=0;
		String sql_r="SELECT cure.*,patient.name,patient.gender,patient.no,patient.birthday,survey.name  as sname  ,disease.name as dname";
		String sql_t="SELECT COUNT(*) ";
		String sql=" FROM `"+POJO.DABASE+"_cure` cure ,`"+POJO.DABASE+"_patient` patient,`"+POJO.DABASE+"_survey` survey ,`"+POJO.DABASE+"_disease` disease WHERE"
				+ " cure.patient=patient.id AND  survey.id=cure.survey AND disease.id=cure.disease  AND cure.main=? AND cure.disease=? ";			
		String sql_o=" ORDER BY cure.id DESC";//排序
		
		Object[] params={main,disease};
		list=QueryHelper.query_page( sql_r+sql+sql_o, page, size, params);			
		total=(int) QueryHelper.stat(sql_t+sql, params);		

		return new PageUtil(list, page, size, total);
	}
	
	/**
	 * 取得治疗对应的身体位置
	 * @param cure 治疗ID
	 * @return
	 */
	public List<Place> getPlace(long cure) {
		String sql="SELECT place.* FROM `"+POJO.DABASE+"_place` place,`"+POJO.DABASE+"_cure_place` cure_place WHERE place.id=cure_place.place AND cure_place.cure=?";
		return QueryHelper.query(Place.class, sql, cure);	
	}
	
	/**
	 * 取得治疗对应的就诊科室
	 * @param cure  治疗ID
	 * @return
	 */
	public List<Department> getDepartment(long cure) {
		String sql="SELECT department.* FROM `"+POJO.DABASE+"_department` department,`"+POJO.DABASE+"_cure_department` cure_department WHERE department.id=cure_department.department AND cure_department.cure=?";
		return QueryHelper.query(Department.class, sql, cure);	
	}
	
	/**
	 * 取得治疗对应的治疗方式
	 * @param cure  治疗ID
	 * @return
	 */
	public List<Treatment> getTreatment(long cure) {
		String sql="SELECT treatment.* FROM `"+POJO.DABASE+"_treatment` treatment,`"+POJO.DABASE+"_cure_treatment` cure_treatment WHERE treatment.id=cure_treatment.treatment AND cure_treatment.cure=?";
		return QueryHelper.query(Treatment.class, sql, cure);	
	}
	
	/**
	 * 取得治疗对应的主管医生
	 * @param cure  治疗ID
	 * @return
	 */
	public List<User> getUser(long cure) {
		String sql="SELECT user.* FROM `"+POJO.DABASE+"_user` user,`"+POJO.DABASE+"_cure_user` cure_user WHERE user.id=cure_user.user AND cure_user.cure=?";
		return QueryHelper.query(User.class, sql, cure);	
	}
	
	/**
	 * 取得需要执行的数据
	 * @param date
	 * @return
	 */
	public List<Cure> getFollowCure(int date) {
		String sql="SELECT cure.* FROM `"+POJO.DABASE+"_cure` cure,`"+POJO.DABASE+"_patient` patient  WHERE patient.id=cure.patient AND  DATE_ADD(NOW(),INTERVAL ? DAY) >= cure.nfdate AND cure.type <>-1 AND patient.alive<>-1";
		return QueryHelper.query(Cure.class, sql,date);
		
	}
}
